package com.aliengen.thingple.driverapp.ui.adapters;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.local.PlanGroupModel;
import com.aliengen.thingple.driverapp.ui.fragments.DeliveryPlanListFragment.OnListFragmentListener;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public class DeliveryPlanListAdapter extends RecyclerView.Adapter<DeliveryPlanListAdapter.ViewHolder> {

    private List<PlanGroupModel> mValues;
    private final OnListFragmentListener mListener;

    public DeliveryPlanListAdapter(OnListFragmentListener listener) {
        mValues = new ArrayList<>();
        this.mListener = listener;
    }

    public void updateList(List<PlanGroupModel> values)
    {
        mValues = values;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        View itemLayoutView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_plan, null);

        ViewHolder viewHolder = new ViewHolder(itemLayoutView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        PlanGroupModel plan = mValues.get(position);
        viewHolder.deliveryPlanReference.setText(plan.plan_reference);
        viewHolder.deliveryPlanDate.setText(plan.date);
        viewHolder.customerName.setText(plan.customer.name);
        viewHolder.customerAddress.setText(plan.customer.address);
        if(position % 2 == 0)
            viewHolder.deliveryPlan.setBackgroundResource(R.color.row_odd);
        else
            viewHolder.deliveryPlan.setBackgroundResource(R.color.row_even);
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }

    public PlanGroupModel getItem(int position) {
        return mValues.get(position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView deliveryPlanReference;
        public final TextView deliveryPlanDate;
        public final TextView customerName;
        public final TextView customerAddress;
        public final RelativeLayout deliveryPlan;

        public ViewHolder(View view) {
            super(view);
            deliveryPlan = (RelativeLayout) view.findViewById(R.id.deliveryplan);
            deliveryPlanReference = (TextView) view.findViewById(R.id.deliveryPlanReference);
            deliveryPlanDate = (TextView) view.findViewById(R.id.deliveryPlanDate);
            customerName = (TextView) view.findViewById(R.id.customerName);
            customerAddress = (TextView) view.findViewById(R.id.customerAddress);
        }
    }
}
